package com.rill.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.rill.po.Article;
import com.rill.service.ArticleClickService;
import com.rill.service.ArticleService;

public class ShowArticleContentAction extends ActionSupport {
	private ArticleService  articleService ;
	private int id;//根据id查文章内容
	//文章点击量 统计
	private ArticleClickService artClickService;

	public ArticleClickService getArtClickService() {
		return artClickService;
	}

	public void setArtClickService(ArticleClickService artClickService) {
		this.artClickService = artClickService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	@Override
	public String execute() throws Exception {
		//先获取request，取得ip地址
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();//获取点击文章的ip
		//按id查文章
		Article article  = articleService.showArticleCountById(id);
		//获取时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String stime = sdf.format(new Date());
		Date time =sdf.parse(stime);
		
		if(!artClickService.isVistor(id, ip, time)){//还没有点击，点击量增加
			article.setHasread(article.getHasread()+1);
		}
		//更新数据表：点击量增加后
		articleService.addArticle(article);
		
		//查出的内容放到request中
		request.setAttribute("article", article);
		return SUCCESS;
	}
}
